home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Documentation / Engineering Notes / Using Edit Views < prev    next >
Encoding:
Text File  |  1996-09-19  |  14.0 KB  |  276 lines  |  [TEXT/ttxt]

  1. OpenDoc
  2. Development
  3. Framework
  4.                                                                                                                                                                                             
  5. Using Edit Views 
  6. ODF Release 2                                                                                                                                                                     
  7.  
  8.  
  9. Table of Contents
  10. ----------------------
  11. • Introduction
  12. • Adding an Edit View to Your Frame
  13. • FW_CEditView Reference
  14. • Adding More Features to FW_CEditView
  15. • Miscellaneous Issues
  16.  
  17.  
  18. Introduction
  19.  
  20. This document explains how to use FW_CEditView to implement text-edit views in your part.  On Macintosh FW_CEditView is a wrapper for TextEdit, the basic text manager described in Inside Macintosh “Text”.  It is well suited for handling editable text items in dialog boxes and small text fields that don’t require formatting.  It supports multi styled text and also the Script manager for non Roman characters.  However it is not intended to manipulate lengthy documents or text requiring more than rudimentary formatting.  If your part requires a real text processor you will need to port existing code you may have or work with a commercial text processor engine.  We plan to provide better text processing support in future releases, including support for TSM, the Text Service Manager.
  21.  
  22. ODF Form is our show-case example for edit views, run it and look at the source code in ODFDev:Form:Sources.
  23.  
  24.  
  25. Adding an Edit View to Your Frame
  26.  
  27. Creation by program or by resource
  28.  
  29. If you are creating your views by program you can use one of the two class constructors:
  30.  
  31.     FW_CEditView (Environment* ev, 
  32.             FW_CSuperView* container, 
  33.             const FW_CRect& bounds,
  34.             ODID  viewId,
  35.             const FW_CString& text, 
  36.             const FW_CFont& font = FW_kNormalFont,
  37.             short maxChars = FW_MAXINT16, 
  38.             unsigned short attributes = FW_CEditView::kDrawBox + 
  39.                     FW_CEditView::kWordWrap + FW_CEditView::kAutoScroll);
  40.  
  41.     FW_CEditView (Environment* ev, 
  42.             FW_CSuperView* container, 
  43.             const FW_CRect& bounds,
  44.    ODID  viewId);                
  45.  
  46. They both require the usual view attributes, container, view id and bounds.  The first one lets you enter an initial string and they both define default values for the other attributes.
  47.  
  48. The following listing shows how the Form example creates its 4 edit views aligned vertically (in CFormView::CreateSubViews) :
  49.  
  50.     viewRect.Set(FW_IntToFixed(290),...);
  51.     FW_CPoint offset(FW_kFixed0, fixed20);
  52.  
  53.     for (int i = 0; i < 4; i++) {
  54.         FW_CEditView* editView = 
  55.                 FW_NEW(FW_CEditView, (ev, this, kFirstNameEdViewID+i, viewRect));
  56.         editView->SetFont(ev, timesBold);
  57.         viewRect += offset;
  58.     }
  59.  
  60. If you are creating your views by resource you define an edit view with the type FW_REditView:
  61.  
  62.     FW_REditView    
  63.     (
  64.         kFirstNameEdViewID,                    // view id
  65.         { FW_FIX(290),FW_FIX(90),FW_FIX(450),FW_FIX(107)},  // bounds
  66.         FW_kFixedBounds,                        // binding
  67.         20,                                // max chars
  68.         9,                                // text attributes
  69.         TIMES_12_BOLD,                        // font
  70.         ""                                // initial text
  71.     ),
  72.  
  73. Edit view attributes
  74.  
  75. The resource fields are the following:
  76.  
  77. • A view id, required to be able to access this object later and read or change its text.
  78.  
  79. • A bounds rectangle, placing the edit box in its parent view.
  80.  
  81. • A binding flag.  It should be initialized to FW_kFixedBounds in order to keep the edit view in a fixed position when its parent  is resized.  You can change that by calling FW_CView::SetBindings or by using a different  value in the resource.  For instance, to make the edit view grow horizontally with its parent use the value FW_kLeftBinding + FW_kRightBinding + FW_kTopBinding + FW_kFixedHeight.  Binding constants are defined in ODF:Framewrk:FWViews:Include:FWViews.k
  82.  
  83. • A maximum number of characters.  A typical 1 line edit field should have a reasonable limit, this prevents your program from having to do extra checking.  A scrolling edit field can use the maximum number allowed by entering -1, this will be converted to FW_MAXINT16 = 32767.
  84.  
  85. • A text attributes flag.   This value is a combination of the enums defined in FW_CEditView (you must use a numeric value in the .fr file because the enums are not defined):
  86.  
  87.     // EditView attributes
  88.     enum AttrEnum
  89.     {
  90.         kDrawBox         = 0x01,    // draw a 1 pixel frame
  91.         kReadOnly        = 0x02,    // not implemented yet
  92.         kWordWrap        = 0x04,    // wrap long lines
  93.         kAutoScroll        = 0x08,    // scroll when text out of view
  94.         kOutlineHilite    = 0x10,    // show selection in inactive text edit
  95.         kTextBuffering    = 0x20,    // for 2-byte scripts
  96.         kInlineInput    = 0x40,    // use keyboard input method
  97.         kTextServices    = 0x80    // see Inside Macintosh: Text
  98.     };
  99.  
  100. Note: see Inside Macintosh:Text for more information on the last 5 flags.
  101.  
  102. For a typical 1 line edit field you should use kDrawBox + kAutoScroll = 9.  By using these constants, you allow the text to scroll horizontally, the right border of the  “destination rectangle” where the text flows is initialized to a large value.
  103.  
  104. For a multi-lines edit view you should use kDrawBox + kWordWrap + kAutoScroll = 13.   When kWordWrap is set the destination rectangle is the same as the view rectangle.
  105.  
  106. • A font object.  The class constructors use FW_kNormalFont by default which maps to Geneva 12 on Macintosh.  In your edit-view resource you can use a macro such as FW_NORMAL_FONT (defined in FWViews.fr) to specify the font, or you can define your own macro at the top of the .fr file:
  107.  
  108.     #define TIMES_12_BOLD    { FW_FIX(12), FW_kBold, "Times" }
  109.  
  110. • The initial text.
  111.  
  112. Text idling
  113.  
  114. Important! The insertion point doesn’t blink until you add an FW_CIdler object to the frame containing the edit view.  Having an idler attached to your part is not enough, only the frame can propagate the null event to its current target, i.e. the active edit view.
  115.  
  116.     CFormFrame::CFormFrame(...)
  117.     {
  118.         // We must create an idler to see the caret blink in text-edit views
  119.         fIdler = FW_NEW(FW_CIdler, (this, 15));
  120.         fIdler->RegisterIdle(ev);                
  121.     }
  122.  
  123.     CFormFrame::~CFormFrame()
  124.     {
  125.         delete fIdler;
  126.     }
  127.  
  128. The exceptions to this rule are dialog frames because an idler object is already created by FW_CDialogFrame.
  129.  
  130. Tabbing between edit views
  131.  
  132. When a frame contains several edit views you can implement easily a tabbing behavior by creating an FW_CViewTabber object.  This cannot be done in resources in the current release, instead do it in your frame’s PostCreateViewFromStream method (or the CreateSubViews method if you are not using resources):
  133.  
  134.     // ----- Add a ViewTabber event handler to the frame 
  135.     fViewTabber = new FW_CViewTabber(ev, this); 
  136.  
  137. You must not delete this object in the frame’s destructor, it will be done for you because this is an attached event handler.
  138.  
  139. A frame containing this event handler traps the Tab key and selects the next view which wants to be the target (or the previous one if the Shift key was pressed too).  This allows the user to circulate among edit views within the same frame, and also circulate among any other view that can respond to keyboard events such as list boxes.  The Tab may be replaced by another key with the FW_CViewTabber::SetTabbingKey method.  The current tab key is returned by GetTabbingKey.
  140.  
  141. Note:  as for text idling, frames derived from FW_CDialogFrame have an FW_CViewTabber object already created for them.
  142.  
  143.  
  144. FW_CEditView Reference
  145.  
  146. Getters methods
  147.  
  148. FW_Boolean     HasBox() const;
  149. FW_Boolean     HasWordWrap() const;
  150. FW_Boolean     HasAutoScroll() const;
  151. unsigned short    GetEditAttributes() const;
  152.  
  153. These methods return  information on the text edit attributes. 
  154.  
  155. FW_CString     GetText(Environment* ev) const;
  156.  
  157. Use GetText to read the content of the edit view in a dynamic string, ODF will adjust its size properly.
  158.  
  159. FW_ByteCount     GetTextLength(Environment * ev) const;
  160.  
  161. Use GetTextLength if you are just interested in the number of bytes currently in the text edit, this avoids making a copy of the text itself.
  162.  
  163. FW_ByteCount    GetMaxChars(Environment* ev) const;
  164.  
  165. GetMaxChars returns the maximum number of bytes allowed in this edit view.  
  166. Note: this is not compatible with 2-byte character scripts, a character may be cut in half when the limit is reached.
  167.  
  168. FW_PlatformHandle    GetPlatformEditHandle(Environment* ev) const;
  169.  
  170. This returns the native Macintosh handle to the text edit record in case you need more internal information.
  171. Warning:  making direct TextEdit manager calls may be dangerous.
  172.  
  173. Text modifications
  174.  
  175. virtual void     SetText(Environment* ev, const FW_CString& text);
  176.  
  177. SetText replaces the entire content with the new text (up to the maximum number of bytes allowed) and redraws the edit view.  The initial text can be set also in the constructor or in resource.
  178.  
  179. virtual void     ClearText(Environment* ev);
  180.  
  181. ClearText empties the edit view and redraws it.
  182.  
  183. virtual void     InsertText(Environment* ev, const FW_CString& textToInsert,                     short offset);
  184.  
  185. This inserts some text after the position offset and redraws the edit view.
  186.  
  187. void             SetSelectedText(Environment* ev, const FW_CString& newText);
  188.  
  189. SetSelectedText is like InsertText but it replaces the current selection.
  190.  
  191. void             SetFont(Environment * ev, const FW_CFont &font);
  192.  
  193. This changes the font and updates the edit view.
  194.  
  195. Text selections
  196.  
  197. FW_CString     GetSelectedText(Environment* ev) const;
  198.  
  199. This returns a dynamic string containing the current selection, or the empty string if nothing is selected.
  200.  
  201. void             GetSelectionRange(Environment* ev, short& startOffset,                         short& endOffset) const;
  202.  
  203. Use GetSelectionRange if you want to get the start and end offsets of the current selection, without copying the text.  An empty selection is when startOffset == endOffset.
  204.  
  205. void             SelectText(Environment* ev, short startOffset, short endOffset)
  206.  
  207. SelectText changes the current selection and updates the edit view.  Use endOffset = FW_MAXINT16 to extend the selection until the end.
  208.  
  209. void             SelectAll(Environment* ev);
  210.  
  211. SelectAll selects the entire text.  This is used by FW_CViewTabber when activating  an edit view during tabbing.
  212.  
  213. Keyboard events
  214.  
  215. virtual FW_Boolean     DoVirtualKey(Environment* ev,                             const FW_CVirtuaKeyEvent& event)
  216. virtual FW_Boolean     DoCharKey(Environment* ev, 
  217.                             const FW_CCharKeyEvent& event)
  218. virtual FW_Boolean     InputCharKey(Environment* ev, char c);
  219.  
  220. DoCharKey and DoVirtualKey are inherited from FW_MEventHandler.  You can override them to handle specific keys. DoCharKey calls InputCharKey with just a char argument, this can be used to modify the character being typed.  See for example CPwdEditView::DoCharKey in the Form sample.
  221.  
  222. virtual void DoTECommand (Environment* ev, ODCommandID id,                         FW_Boolean useScrap);
  223.  
  224. This is called in response to an Edit menu command: Cut, Copy, Paste, Clear and SelectAll.   useScrap is true when the Macintosh Scrap is used instead of the OpenDoc clipboard.  The Form’s class CEditViewCommand uses doTECommand to implement undo/redo actions.
  225.  
  226.  
  227. Adding More Features to FW_CEditView
  228.  
  229. Key filters
  230.  
  231. Two event handler classes allow you to filter out all characters not belonging to a specific set.  The following line attaches an event handler accepting only alpha-numeric characters (plus backspace and the arrow keys):
  232.  
  233. FW_CAlphaNumFilter* filter1 = new FW_CAlphaNumFilter(ev, myEditView);
  234.  
  235. In the same way this line attaches an event handler accepting only numeric characters:
  236.  
  237. FW_CIntegerFilter* filter2 = new FW_CIntegerFilter(ev, myEditView);
  238.  
  239. Note: attached event handlers are deleted automatically with their parent. 
  240.  
  241. Invisible characters
  242.  
  243. The Form sample contains a password dialog with an edit view class that replaces each character with a “bullet”.  A separate invisible edit view contains the real text being edited so that backspace and arrow keys are supported.  See the file :ODFDev:Form:Sources:Dialog.cpp.   
  244.  
  245. Scrolling
  246.  
  247. Another custom class in Form, CScrollEdit, demonstrates how to add scroll bars to an edit view  (the ODF classes FW_CScroller and FW_CScrollBarScroller  cannot be used because they handle only the content view in this release).  You can reuse CScrollEdit directly in your part.   See the file :ODFDev:Form:Sources:ScrollEd.cpp.   
  248.  
  249. Undo/Redo support
  250.  
  251. Finally, Form provides an example of Undo/Redo support in edit views with the classes: CEditViewCommand, CEditViewSelection and CEditViewSelContent.   See the file :ODFDev:Form:Sources:EditCmd.cpp.  
  252.  
  253. These classes allow you to support Undo/Redo actions even if the edit views do not belong to your part’s content.  In ODF version 1 the Form sample doesn’t maintain any content made of the form’s data, the Form’s presentation doesn’t have any selection object; in this case you must create a local selection object for each edit view as it is done in the method CScrollEdit::DoMenu. The problem is that an edit view may be deleted (when the frame is closed) while its associated Undo/Redo commands are still on the OpenDoc Undo/Redo stacks.  The solution we use is to reset the command’s fEditView field to 0 when it is notified of the edit view going away (see CEditViewCommand::HandleNotification), the command will still appear in the Edit menu but it will do nothing.
  254.  
  255. Content model and Selections
  256.  
  257. If you decide to make an edit view part of your content model, you need to manage the presentation’s selection object as the user selects text and you need to externalize/internalize the data.  For an example see the “non official” sample part Intl Test in the Tools & Goodies folder.
  258.  
  259. 2-byte character support
  260.  
  261. The same Intl Test part in Tools & Goodies shows how to use Japanese script and input method.
  262.  
  263.  
  264. Miscellaneous Issues
  265.  
  266. Multiple Facets
  267.  
  268. When an edit view is visible in more than 1 facet you must force the redraw of all the other facets to see the correct text (e.g. the only facet updated by ODF Draw is the active facet receiving keyboard events).
  269.  
  270. Multiple Frames
  271.  
  272. The edit view’s content, its text, is local to the frame because it is stored by in the platform’s text edit record.  If you open 2 frames of the same presentation, for example with the “View In Window” command, each frame contains its own edit views and it is up to your part to maintain a common content when it makes sense to do so.
  273.  
  274.  
  275. © 1993 - 1996 Apple Computer, Inc. All rights reserved.
  276. Apple, the Apple Logo, Macintosh, and OpenDoc are trademarks of Apple Computer, Inc., registered in the United States and other countries.